/*******************************************************
> Copyright (C) 2024 ==IOT== All rights reserved.
> File Name: likangfee.c
> Author: lv1
> Website:www.lv1.org
> Created Time: 2024年12月06日 星期五 14时43分04秒
***************************************************/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include "cJSON.h"
#include <mysql/mysql.h>

/*
用户姓名	username	varchar(20)
用药费用	medicine_fee	int
住宿费用	month_fee	int
总费用	    total_fee	int
账户余额	account_fee	int
欠费金额	owing	int
最近时间	recent_time	varchar(35)
*/
char* packJson()
{
	cJSON* root = cJSON_CreateObject();
	
	char name[20] = {0};
	printf("input 用户姓名: ");
    scanf("%s", name);
	cJSON* cjson_name = cJSON_CreateString(name);
	
	int medicine_fee = 0;
	printf("input 用药费用: ");
    scanf("%d", &medicine_fee);
    cJSON* cjson_medicine_fee = cJSON_CreateNumber(medicine_fee);
	
	int month_fee = 2000;
	cJSON* cjson_month_fee = cJSON_CreateNumber(month_fee);
	
	int total_fee = medicine_fee + month_fee;
	cJSON* cjson_total_fee = cJSON_CreateNumber(total_fee);
	
	//初始账户余额为0
	cJSON* cjson_account_fee = cJSON_CreateNumber(0);
	
	int owing  = 0;
	owing = total_fee;
	cJSON* cjson_owing = cJSON_CreateNumber(owing);
	
	cJSON* cjson_recent_time = cJSON_CreateString("0");
	
	cJSON_AddItemToObject(root, "name", cjson_name);
	cJSON_AddItemToObject(root, "medicine_fee", cjson_medicine_fee);
	cJSON_AddItemToObject(root, "month_fee", cjson_month_fee);
	cJSON_AddItemToObject(root, "total_fee", cjson_total_fee);
	cJSON_AddItemToObject(root, "account_fee", cjson_account_fee);
	cJSON_AddItemToObject(root, "owing", cjson_owing);
	cJSON_AddItemToObject(root, "recent_time", cjson_recent_time);
	
	char *p = (char *)malloc(1024);
	p = cJSON_Print(root);

	printf("%s\n",p);
	cJSON_Delete(root);
	return p;
}

void parseJson(char*p){
	
	cJSON* root = cJSON_Parse(p);
	
	cJSON *name = cJSON_GetObjectItem(root,"name");
	cJSON *medicine_fee = cJSON_GetObjectItem(root,"medicine_fee");
	cJSON *month_fee = cJSON_GetObjectItem(root,"month_fee");
	cJSON *total_fee = cJSON_GetObjectItem(root,"total_fee");
	cJSON *account_fee = cJSON_GetObjectItem(root,"account_fee");
	cJSON *owing = cJSON_GetObjectItem(root,"owing");
	cJSON *recent_time = cJSON_GetObjectItem(root,"recent_time");
	
	char *name1 = cJSON_GetStringValue(name);
	ssize_t medicine_fee1 = cJSON_GetNumberValue(medicine_fee);
	ssize_t month_fee1 = cJSON_GetNumberValue(month_fee);
	ssize_t total_fee1 = cJSON_GetNumberValue(total_fee);
	ssize_t account_fee1 = cJSON_GetNumberValue(account_fee);
	ssize_t owing1 = cJSON_GetNumberValue(owing);
	char *recent_time1 = cJSON_GetStringValue(recent_time);
	
	printf("name='%s',medicine_fee=%ld,month_fee=%ld,total_fee=%ld,account_fee=%ld,owing=%ld,recent_time='%s'",\
	name1,medicine_fee1,month_fee1,total_fee1,account_fee1,owing1,recent_time1);
	cJSON_Delete(root);
	
	MYSQL mysql;
	mysql_init(&mysql);
	MYSQL *flag;
	flag = mysql_real_connect(&mysql,"localhost","root","1","care",0,NULL,0);
	char addsql[1000]={0};
	sprintf(addsql,"insert into fee_details(username,medicine_fee,month_fee,total_fee,account_fee,owing,recent_time)\
	values('%s',%ld,%ld,%ld,%ld,%ld,'%s')",name1,medicine_fee1,month_fee1,total_fee1,account_fee1,owing1,recent_time1);
	mysql_query(&mysql,addsql);
	
	mysql_close(&mysql);
	
}
	
	int main(int argc,const char* argv[])
{
	int fp = socket(AF_INET,SOCK_DGRAM,0);
	
	char*p = packJson();
	parseJson(p);
	free(p);
	close(fp);
	
    return 0;
}